package com.example.chapter0603;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager manager = getSupportFragmentManager();
        IndexFragment fragment = new IndexFragment();
        manager.beginTransaction()  //开启一个新事务
                .add(R.id.fragment, fragment)
                //添加Fragment到Activity，第一个参数是要把Fragment添加到的容器Id，第二个是要添加的Fragment
                .commit();   //提交事务，否则添加就没成功
        findViewById(R.id.index).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                manager.beginTransaction()  //开启一个新事务
                        .replace(R.id.fragment, fragment)
                        //添加Fragment到Activity，第一个参数是要把Fragment添加到的容器Id，第二个是要添加的Fragment
                        .commit();   //提交事务，否则添加就没成功

            }
        });
        findViewById(R.id.contact).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager manager = getSupportFragmentManager();
                ContactFragment fragment = new ContactFragment();
                manager.beginTransaction()  //开启一个新事务
                        .replace(R.id.fragment, fragment)
                        //添加Fragment到Activity，第一个参数是要把Fragment添加到的容器Id，第二个是要添加的Fragment
                        .commit();   //提交事务，否则添加就没成功
            }
        });
        findViewById(R.id.friends).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager manager = getSupportFragmentManager();
                FriendsFragment fragment = new FriendsFragment();
                manager.beginTransaction()  //开启一个新事务
                        .replace(R.id.fragment, fragment)
                        //添加Fragment到Activity，第一个参数是要把Fragment添加到的容器Id，第二个是要添加的Fragment
                        .commit();   //提交事务，否则添加就没成功
            }
        });
        findViewById(R.id.me).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager manager = getSupportFragmentManager();
                MeFragment fragment = new MeFragment();
                manager.beginTransaction()  //开启一个新事务
                        .replace(R.id.fragment, fragment)
                        //添加Fragment到Activity，第一个参数是要把Fragment添加到的容器Id，第二个是要添加的Fragment
                        .commit();   //提交事务，否则添加就没成功
            }
        });
    }
}